package com.sky.controller.user;

import com.sky.exception.BaseException;
import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@ApiOperation(value = "小程序-用户端的店铺状态相关接口")
@RequestMapping("/user/shop")
@CrossOrigin//解决跨域问题
public class UserShopStatusController {
    //注入redisTemplate模版对象用来操作redis缓存数据
    @Autowired
    private RedisTemplate redisTemplate;
    //创建一个常量,用来存放redis缓存的key值
    public static final String SHOP_STATUS_KEY = "shop_status";
    /**
     * 小程序用户端-获取店铺营业状态
     */
    @ApiOperation(value = "获取店铺营业状态", notes = "获取店铺营业状态")
    @GetMapping("/status")
    public Result<Integer> getUserShopStatus() {
        //1.记录日志
        log.info("获取用户端店铺营业状态");
        //2.业务获取店铺营业状态
        String shopStatus = (String) redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        if (shopStatus == null|| shopStatus.isEmpty()) {
            throw new BaseException("店铺营业状态获取失败");
        }
        //3.返回统一的响应结果对象result
        return Result.success(Integer.parseInt(shopStatus));
    }
}
